class TempUser < ActiveRecord::Base
  belongs_to :org , :class_name => "User", :foreign_key => "org_id"
  belongs_to :folder,:class_name => "Folder", :foreign_key => "folder_id"
  #has_and_belongs_to_many :surveys, :join_table => "temp_users_surveys"
  validates_presence_of :name
  validates_presence_of :password
  validates_presence_of :folder_id
  validates_presence_of :org_id
  validates_presence_of :last_time
  validates_presence_of :survey_ids
      
  def self.authenticate(name, passwd)
    self.find_by_name_and_password(name, passwd)
  end

  def surveys
    if survey_ids
      Survey.find(survey_ids.split(','))
    else
      nil
    end
  end

  def tested(survey_id)
    if survey_ids
      arr = survey_ids.split(',')
      arr.delete(survey_id)
      survey_ids = arr.join(',')
      update_attribute(:survey_ids, survey_ids) 
    end
  end
end

